/**
 * jims
 */
package com.jims.master.modules.base.order.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.base.order.dao.SordersDao;
import com.jims.master.modules.base.order.entity.Sorders;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.base.user.service.MasterDoctorinfoService;
import com.jims.master.modules.patient.myFriend.entity.SysUserMessage;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单信息Service
 *
 * @author qlx
 * @version 2017-05-02
 */
@Service
public class SordersService extends CrudService<SordersDao, Sorders> {
    @Autowired
    private MasterDoctorinfoService masterDoctorinfoService;
    /**
     * 获取退款列表
     * @param page
     * @param sorders
     * @return
     */
    public Page<Sorders> findDrawBackList(Page<Sorders> page, Sorders sorders) {
        sorders.setPage(page);
        page.setList(dao.findDrawBackList(sorders));
        return page;
    }

    /**
     * 获取订单号
     * @return
     */
    public String getOrderNo() {
        return dao.getOrderNo();
    }

    /**
     * 累计服务人数
     * @author chent
     * @return
     */
    public String personOrderCount() {
        return dao.personOrderCount();
    }

    /**
     * 医生中心我的购买服务量
     * @author jicunteng
     * @return
     */
    public String buyOrderCount(String doctorId) {
        return dao.buyOrderCount(doctorId);
    }

    /**
     * 根据系统服务ID得到签约人数
     *
     * @param defaultId
     * @return
     */
    public String orderCountByDefaultId(String defaultId) {
        return dao.orderCountByDefaultId(defaultId);
    }

    /**
     * 通过订单号查找订单
     * @param orderNo
     * @return
     */
    public Sorders getSordersByOrderNo(String orderNo) {
        return dao.getSordersByOrderNo(orderNo);
    }

    /**
     * 修改訂單
     * @param orderNo
     * @param buyEmail
     * @param alipyNo
     * @param alipyStyle
     * @return
     */
    @Transactional(readOnly = false)
    public int updateStateByOrder(String orderNo,String buyEmail,String alipyNo,String alipyStyle) {
        return dao.updateStateByOrder(orderNo,buyEmail,alipyNo,alipyStyle);
    }

    /**
     * 更新退款狀態
     * @param orderNo
     * @param orderState
     * @return
     */
    @Transactional(readOnly = false)
    public int updateDrawBackStateByOrder(String orderNo,String orderState) {
        return dao.updateDrawBackStateByOrder(orderNo,orderState);
    }

    /**
     * 我的服务
     * @param baseinfoId
     * @return
     */
    @Transactional(readOnly = true)
    public List<Sorders> myService(String baseinfoId) {
        List<Sorders> sordersList=dao.myService(baseinfoId);
        List newSordersList= new ArrayList<Sorders>();
        if(sordersList.size()>0){
            for(int i=0;i<sordersList.size();i++){
                Sorders sorders=sordersList.get(i);
                MasterDoctorinfo masterDoctorinfo=masterDoctorinfoService.get(sorders.getDoctorId());
                sorders.setMasterDoctorinfo(masterDoctorinfo);
                newSordersList.add(sorders);
            }
        }
        return newSordersList;
    }
    /**
     * 医生收入
     * @param doctorId
     * @return
     */
    @Transactional(readOnly = true)
    public List<Sorders> doctorIncome(String doctorId) {
        List<Sorders> sordersList=dao.doctorIncome(doctorId);
        /*double income=0.00;
        for(int i=0;i<sordersList.size();i++){
               income=income+sordersList.get(i).getCost();
        }*/
        return sordersList;
    }


    public List<String> getPatientId(String doctorId) {
        return dao.getPatientId(doctorId);
    }

    @Transactional(readOnly = true)
    public List<Sorders> patientList(Sorders sorders) {
        return dao.patientList(sorders);
    }

    public Page<Sorders> getPatientServer(Page<Sorders> page, Map map) {
        page.setPageNo(Integer.parseInt((String) map.get("pageNo")));
        if(StringUtils.isNotBlank((String) map.get("pageSize"))){
            page.setPageSize(Integer.parseInt((String) map.get("pageSize")));
        }else{
            page.setPageSize(10);
        }
        Sorders sorders = new Sorders();
        sorders.setPersonId((String)map.get("userId"));
        sorders.setPage(page);
        page.setList(this.dao.getPatientServer(sorders));
        return page;
    }
}